package com.campus.config.handler;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.campus.common.RoleTypeConstants;
import com.campus.mapper.StudentMapper;
import com.campus.mapper.UserRoleMapper;
import com.campus.mapping.ManagerStudentMapping;
import com.campus.model.TManagerStudent;
import com.campus.model.TSysUserRole;
import com.campus.model.vo.StudentExcelVo;
import com.campus.model.vo.StudentUploadVo;
import com.campus.service.IStudentService;
import com.campus.service.IUserRoleService;
import com.campus.service.impl.StudentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
public class StudentListener extends AnalysisEventListener<StudentUploadVo> {

    private static final int MAX_ROW_PER_READ = 5;
    private IStudentService studentService;

    private IUserRoleService userRoleService;
    private List<StudentUploadVo> cachedReadData = new ArrayList<>(MAX_ROW_PER_READ);

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        // headMap 包含了表头的数据，键为列索引，值为列标题
        System.out.println("表头数据：" + headMap);
    }

    public StudentListener(IStudentService studentService,IUserRoleService userRoleService) {
        this.studentService = studentService;
        this.userRoleService = userRoleService;
    }

    @Override
    public void invoke(StudentUploadVo studentUploadVo, AnalysisContext analysisContext) {
        try {
            log.info("读取到一条学生信息：{}", studentUploadVo);
            cachedReadData.add(studentUploadVo);
            if (cachedReadData.size() >= MAX_ROW_PER_READ) {
                saveToDatabase();
            }
        } catch (Exception e) {
            log.error("处理数据时发生错误", e);
        }
    }


    private void saveToDatabase() {
        log.info("数据写入数据库：{}", cachedReadData);
        for (StudentUploadVo uploadDto : cachedReadData) {


            TManagerStudent managerStudent = ManagerStudentMapping.INSTANCE.updateVoToStudent(uploadDto);
            TSysUserRole userRole = new TSysUserRole();
            userRole.setUserId(uploadDto.getLoginId());
            userRole.setRoleId(RoleTypeConstants.STUDENT);
            userRoleService.save(userRole);
            studentService.save(managerStudent);
        }
        cachedReadData.clear();
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        try {
            if (!cachedReadData.isEmpty()) {
                saveToDatabase();
            }
            log.info("读取完毕");
        } catch (Exception e) {
            log.error("处理剩余数据时发生错误", e);
        }
    }
}